<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="80%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="工艺名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="工艺名称"></el-input>
    </el-form-item>
<!--    <el-form-item label="工艺编号" prop="code">-->
<!--      <el-input v-model="dataForm.code" placeholder="工艺编号"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="工艺价格(元)" prop="price">
<!--      <el-input v-model="dataForm.price" placeholder="工艺价格"></el-input>-->
      <el-input-number v-model="dataForm.price" controls-position="right"
                       :precision="2" :step="0.1" :min="0"></el-input-number>
    </el-form-item>
<!--    <el-form-item label="删除标志，0：未删除，1：删除" prop="deleted">-->
<!--      <el-input v-model="dataForm.deleted" placeholder="删除标志，0：未删除，1：删除"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="租户ID" prop="tenantId">-->
<!--      <el-input v-model="dataForm.tenantId" placeholder="租户ID"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="创建者ID" prop="createUserId">-->
<!--      <el-input v-model="dataForm.createUserId" placeholder="创建者ID"></el-input>-->
<!--    </el-form-item>-->
<!--    <el-form-item label="创建时间" prop="createTime">-->
<!--      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          code: '',
          price: '',
          deleted: '',
          tenantId: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '工艺名称不能为空', trigger: 'blur' }
          ],
          // code: [
          //   { required: true, message: '工艺编号不能为空', trigger: 'blur' }
          // ],
          price: [
            { required: true, message: '工艺价格不能为空', trigger: 'blur' }
          ]
          // ,
          // deleted: [
          //   { required: true, message: '删除标志，0：未删除，1：删除不能为空', trigger: 'blur' }
          // ],
          // tenantId: [
          //   { required: true, message: '租户ID不能为空', trigger: 'blur' }
          // ],
          // createUserId: [
          //   { required: true, message: '创建者ID不能为空', trigger: 'blur' }
          // ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sto/crafts/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.crafts.name
                this.dataForm.code = data.crafts.code
                this.dataForm.price = data.crafts.price
                this.dataForm.deleted = data.crafts.deleted
                this.dataForm.tenantId = data.crafts.tenantId
                this.dataForm.createUserId = data.crafts.createUserId
                this.dataForm.createTime = data.crafts.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sto/crafts/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'code': this.dataForm.code,
                'price': this.dataForm.price,
                'deleted': this.dataForm.deleted,
                'tenantId': this.dataForm.tenantId,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
